//@author A0101906R
#include "CmdDel.h"

using namespace Twentyfifth;
using namespace std;

const std::string CmdDel::FEEDBACK_DELOK = "Task successfully deleted.\n";
const std::string CmdDel::FEEDBACK_UNDODELOK = "Undo delete successful.\n";
//@author A0101906R
CmdDel::CmdDel() {
	cmdType = DEL;
}
//@author A0101906R
Status CmdDel::execution() {
	Status deleteStatus;
	Status tempS;
	assert(storage != NULL);
	int id;
	//to remember the task before deleting, for undo purpose
	rememberTask(tempS);
	id = task.getId();

	deleteStatus = storage->deleteTask(id);
	setDescForDel(deleteStatus);

	return deleteStatus;
}
//@author A0101906R
Status CmdDel::undo(Status::StatusCode c) {
	Status undoStatus;
	assert(storage != NULL);
	try {
		if (c != Status::OK) {
			throw ERROR_UNSUCCESSFUL;
		}
		int taskId = task.getId();
		undoStatus = storage->undeleteTask(taskId);
		setDescForUndo(undoStatus);
	}
	catch (string error) {
		undoStatus.desc = error;
	}
	return undoStatus;
}
//@author A0101906R
void CmdDel::rememberTask(Status& s) {
	int id;
	id = task.getId();
	s = storage->viewTask(id);
	if (s.code == Status::OK) {
		assert(s.tasks.size() != 0);
		taskBeforeDel = s.tasks[0];
		task = taskBeforeDel;
	}
	return;
}
//@author A0101906R
void CmdDel::setDescForDel(Status& s) {
	if (s.code == Status::OK) {
		s.desc = FEEDBACK_DELOK;
	} else {
		s.desc = statusCodeToString(s);
	}
	return;
}
//@author A0101906R
void CmdDel::setDescForUndo(Status& s) {
	if (s.code == Status::OK) {
		s.desc = FEEDBACK_UNDODELOK;
	} else {
		s.desc = statusCodeToString(s);
	}
	return;
}